<template>
  <el-dialog title="新增角色" width="840px" :visible="visible" @close="handleClose">
    <div class="white-card">
      <el-form ref="form" :model="form" :rules="rules" size="small" label-width="110px">
        <el-form-item label="角色名称：" prop="roleName">
          <el-input v-model="form.roleName" placeholder="请输入"></el-input>
        </el-form-item>
        <el-form-item label="角色描述：" prop="">
          <el-input v-model="form.remark" placeholder="请输入"></el-input>
        </el-form-item>
        <!-- <el-form-item label="菜单权限：" prop="menuIds">
          <el-cascader v-model="form.menuIds" :options="cascaderList" :show-all-levels="false" :props="cascaderProps" @change="handleChange" style="width: 100%" clearable ref="cascaderRef"></el-cascader>
        </el-form-item> -->
      </el-form>

      <div class="dialog-footer-btn">
        <el-button @click="handleSubmit" :loading="submitting" type="primary">确定</el-button>
        <el-button @click="handleClose" type="primary" plain>取消</el-button>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import * as roleApi from "@/api/uias/role";
import * as mixins from "@/utils/mixins";
import * as validators from "@/utils/validators";

export default {
  name: "setCompetentDepartmentDialog",
  mixins: [mixins.dialog, mixins.form],
  props: {
    dialogData: { type: Object, default: Object },
    cascaderList: { type: Array, default: Array },
  },
  data() {
    return {
      form: {
        id: "",
        roleName: "",
        remark: "",
        status: 1,
      },
      rules: {
        roleName: [{ required: true, message: "请输入" }],
        menuIds: [{ required: true, message: "请选择" }],
      },
      hasError: false,
      submitting: false,
      menuTypeOptions: [
        { label: "是", value: 1 },
        { label: "否", value: 0 },
      ],
      statusOptions: [
        { label: "启用", value: 1 },
        { label: "禁用", value: 0 },
      ],
      cascaderProps: { expandTrigger: "hover", checkStrictly: true, emitPath: false, value: "id", label: "menuName", children: "menuVOS", multiple: true },
    };
  },
  watch: {
    form: {
      handler() {
        this.hasError = validators.validate(this.form, this.rules);
      },
      deep: true,
      immediate: true,
    },
  },
  created() {},
  methods: {
    show() {
      this.form = Object.assign(this.form, this.dialogData);
    },
    handleSubmit() {
      this.submitting = true;
      this.validateAndConfirm("form")
        .then(() => {
          const form = {
            id: this.form.id,
            remark: this.form.remark,
            roleName: this.form.roleName,
            status: this.form.status,
          };
          roleApi.addUpdateRole(form).then((res) => {
            if (res.code === 0) {
              this.$message.success("操作成功");
              this.$emit("success");
              this.handleClose();
            }
          });
        })
        .finally(() => (this.submitting = false));
    },
    handleCascaderChange() {
      this.$refs.cascader.toggleDropDownVisible(false);
    },
    handleChange() {
      let nodesObj = this.$refs["cascaderRef"].getCheckedNodes();
      const menuIds = [];
      nodesObj.forEach((item) => {
        menuIds.push(item.data.id);
        if (item.data.menuIds !== "-1") {
          menuIds.push(item.data.menuIds);
        }
      });
      menuIds.sort();
      this.form.menuIds = [...new Set(menuIds)];
    },
  },
};
</script>

<style scoped></style>
